package com.esun.payment_api.service.impl;

import com.esun.common.api.CommonPage;
import com.esun.common.api.CommonResult;
import com.esun.common.bean.PageRequest;
import com.esun.common.utils.SecurityUtils;
import com.esun.payment_api.domain.*;
import com.esun.payment_api.mapper.CompanyMapper;
import com.esun.payment_api.mapper.DepartmentMapper;
import com.esun.payment_api.service.UnitService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UnitServiceImpl implements UnitService {

    @Resource
    CompanyMapper companyMapper;

    @Resource
    DepartmentMapper departmentMapper;

    public CommonResult getCompanyPage(PageRequest pageRequest){
        try {
            CompanyExample companyExample = new CompanyExample();
            CompanyExample.Criteria companyExampleCriteria = companyExample.createCriteria();
            if(pageRequest!=null&&pageRequest.getConditions()!=null&&pageRequest.getConditions().get("name")!=null){
                companyExampleCriteria.andNameLike("%" + pageRequest.getConditions().get("name") + "%");
            }
            companyExample.setOrderByClause("id desc");
            List<Company> companyList = companyMapper.selectByExample(companyExample);
            CommonPage commonPage = new CommonPage<>();
            commonPage.setList(companyList);
            return CommonResult.success(commonPage, "读取公司分页表成功");
        } catch (Exception e){
            return CommonResult.failed("读取公司分页表失败，参考原因" + e.getMessage());
        }
    }

    public CommonResult addCompany(Company company){
        if(companyMapper.insertSelective(company)!=1){
            return CommonResult.failed("新建公司失败");
        } else {
            return CommonResult.success(null, "新建公司成功，公司名"+company.getName());
        }
    }

    public CommonResult editCompany(Company company){
        if(companyMapper.insertSelective(company)!=1){
            return CommonResult.failed("更新公司失败");
        } else {
            return CommonResult.success(null, "更新公司完成，公司id"+company.getId());
        }
    }

    public CommonResult deleteCompany(Short companyId){
        //警告：由于公司数据表中暂未设置is_deleted的软删判断，现在是直接删除！
        if(companyMapper.deleteByPrimaryKey(companyId)!=1){
            return CommonResult.failed("删除公司失败");
        } else {
            return CommonResult.success(null, "成功删除公司，公司id"+companyId);
        }
    }


    public CommonResult getDepartmentPage(PageRequest pageRequest){
        try {
            DepartmentExample departmentExample = new DepartmentExample();
            DepartmentExample.Criteria departmentExampleCriteria = departmentExample.createCriteria();
            if(pageRequest!=null&&pageRequest.getConditions()!=null&&pageRequest.getConditions().get("name")!=null){
                departmentExampleCriteria.andDeptNameLike("%" + pageRequest.getConditions().get("name") + "%");
            }
            departmentExampleCriteria.andIsDeletedEqualTo("0");
            departmentExample.setOrderByClause("id desc");
            List<Department> departmentList = departmentMapper.selectByExample(departmentExample);
            CommonPage commonPage = new CommonPage<>();
            commonPage.setList(departmentList);
            return CommonResult.success(commonPage, "读取公司分页表成功");
        } catch (Exception e){
            return CommonResult.failed("读取公司分页表失败，参考原因" + e.getMessage());
        }
    }

    public CommonResult addDepartment(Department department){
        if(departmentMapper.insertSelective(department)!=1){
            return CommonResult.failed("新建部门失败");
        } else {
            return CommonResult.success(null, "新建部门成功，部门名"+department.getDeptName());
        }
    }

    public CommonResult editDepartment(Department department){
        if(departmentMapper.insertSelective(department)!=1){
            return CommonResult.failed("更新部门失败");
        } else {
            return CommonResult.success(null, "更新部门完成，部门id"+department.getId());
        }
    }

    public CommonResult deleteDepartment(Short departmentId){
        Department department = departmentMapper.selectByPrimaryKey(departmentId);
        if(department==null){
            return CommonResult.failed("未找到要删除的部门（id为"+departmentId+"）");
        } else {
            department.setIsDeleted("1");
            if(departmentMapper.updateByPrimaryKeySelective(department)!=1){
                return CommonResult.failed("删除部门失败");
            } else {
                return CommonResult.success(null, "删除部门成功，部门id为" + departmentId);
            }
        }
    }
}
